package turtle.system.doug.turtleaction;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.TreeMap;

import turtle.system.pt.StatCalc;
import arbor.mining.util.date.DateUtil;

public class ProfitAnalyzer {
	   private static final DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
	   
		int LOOK_DAY_THRESHOLD = 10;
		TreeMap<String, Double> dayProfitMap;
		TreeMap<String, Double> maxMap = new TreeMap<String, Double>();
		TreeMap<String, Double> minMap = new TreeMap<String, Double>();

		public ProfitAnalyzer(TreeMap<String, Double> dayProfitMap) {
			this.dayProfitMap = dayProfitMap;
		}

		public void analyzeBaseStatistic() throws ParseException, IOException {
			BufferedWriter writer = new BufferedWriter(new FileWriter(
					"tmp/year_evaluate.csv"));
			String leastDayInDB = dayProfitMap.lastKey();
			StatCalc sixMonthProfit = new StatCalc();
			StatCalc oneYearProfit = new StatCalc();
			StatCalc twoYearProfit = new StatCalc();
			StatCalc threeYearProfit = new StatCalc();

			StatCalc sixMonthMin = new StatCalc();
			StatCalc oneYearMin = new StatCalc();
			StatCalc twoYearMin = new StatCalc();
			StatCalc threeYearMin = new StatCalc();

			StatCalc sixMonthMax = new StatCalc();
			StatCalc oneYearMax = new StatCalc();
			StatCalc twoYearMax = new StatCalc();
			StatCalc threeYearMax = new StatCalc();

			SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMdd");
			GregorianCalendar sixMonthDay = new GregorianCalendar();
			GregorianCalendar oneYearDay = new GregorianCalendar();
			GregorianCalendar twoYearDay = new GregorianCalendar();
			GregorianCalendar threeYearDay = new GregorianCalendar();
			GregorianCalendar leastDB = new GregorianCalendar();
			leastDB.setTime(df.parse(leastDayInDB));
			Iterator<String> iter = dayProfitMap.keySet().iterator();

			writer
					.write("Date,SixMonthDuringMin,SixMonthDuringMax,SixMonthProfit,OneYearDuringMin,OneYearDuringMax,OneYearProfit,"
							+ "TwoYearDuringMin,TwoYearDuringMax,TwoYearProfit,ThreeYearDuringMin,ThreeYearDuringMax,ThreeYearProfit\n");
			while (iter.hasNext()) {
				String today = iter.next();
				if (!iter.hasNext()) {
					continue;
				}
				// System.out.println(today);
				if (!dayProfitMap.containsKey(today)) {
					continue;
				}
				double todayVal = dayProfitMap.get(today);
				writer.write(today + ",");
				Date d = df.parse(today);
				sixMonthDay.setTime(d);
				oneYearDay.setTime(d);
				twoYearDay.setTime(d);
				threeYearDay.setTime(d);
				sixMonthDay.add(Calendar.MONTH, 6);
				oneYearDay.add(Calendar.YEAR, 1);
				twoYearDay.add(Calendar.YEAR, 2);
				threeYearDay.add(Calendar.YEAR, 3);
				String[] between;
				StatCalc tmp;
				if (!sixMonthDay.after(leastDB)) {
					between = DateUtil.getDateBetween(df1.format(d), df1
							.format(sixMonthDay.getTime()));
					tmp = new StatCalc();
					for (int i = 0; i < between.length; i++) {
						String b = df.format(df1.parse(between[i]));
						if (dayProfitMap.containsKey(b)) {
							tmp.enter(dayProfitMap.get(b) - todayVal);
						}
					}
					sixMonthProfit.enter(tmp.getLeastNum());
					sixMonthMin.enter(tmp.getMin());
					sixMonthMax.enter(tmp.getMax());
					writer.write(tmp.getMin() + "," + tmp.getMax() + ","
							+ tmp.getLeastNum() + ",");
				} else {
					writer.write(",,,");
				}
				if (!oneYearDay.after(leastDB)) {
					between = DateUtil.getDateBetween(df1.format(d), df1
							.format(oneYearDay.getTime()));
					tmp = new StatCalc();
					for (int i = 0; i < between.length; i++) {
						String b = df.format(df1.parse(between[i]));
						if (dayProfitMap.containsKey(b)) {
							tmp.enter(dayProfitMap.get(b) - todayVal);
						}
					}
					oneYearProfit.enter(tmp.getLeastNum());
					oneYearMin.enter(tmp.getMin());
					oneYearMax.enter(tmp.getMax());
					writer.write(tmp.getMin() + "," + tmp.getMax() + ","
							+ tmp.getLeastNum() + ",");
				} else {
					writer.write(",,,");
				}
				if (!twoYearDay.after(leastDB)) {
					between = DateUtil.getDateBetween(df1.format(d), df1
							.format(twoYearDay.getTime()));
					tmp = new StatCalc();
					for (int i = 0; i < between.length; i++) {
						String b = df.format(df1.parse(between[i]));
						if (dayProfitMap.containsKey(b)) {
							tmp.enter(dayProfitMap.get(b) - todayVal);
						}
					}
					twoYearProfit.enter(tmp.getLeastNum());
					twoYearMin.enter(tmp.getMin());
					twoYearMax.enter(tmp.getMax());
					writer.write(tmp.getMin() + "," + tmp.getMax() + ","
							+ tmp.getLeastNum() + ",");
				} else {
					writer.write(",,,");
				}
				if (!threeYearDay.after(leastDB)) {
					between = DateUtil.getDateBetween(df1.format(d), df1
							.format(threeYearDay.getTime()));
					tmp = new StatCalc();
					for (int i = 0; i < between.length; i++) {
						String b = df.format(df1.parse(between[i]));
						if (dayProfitMap.containsKey(b)) {
							tmp.enter(dayProfitMap.get(b) - todayVal);
						}
					}
					threeYearProfit.enter(tmp.getLeastNum());
					threeYearMin.enter(tmp.getMin());
					threeYearMax.enter(tmp.getMax());

					writer.write(tmp.getMin() + "," + tmp.getMax() + ","
							+ tmp.getLeastNum() + "\n");
				} else {
					writer.write(",,\n");
				}
			}

			String line = "六個月平均獲利:" + sixMonthProfit.getMean() + "\n六個月最少獲利:"
					+ sixMonthProfit.getMin() + "\n六個月最高獲利:"
					+ sixMonthProfit.getMax() + "\n一年平均獲利:"
					+ oneYearProfit.getMean() + "\n一年最少獲利:"
					+ oneYearProfit.getMin() + "\n一年最高獲利:"
					+ oneYearProfit.getMax() + "\n兩年平均獲利:"
					+ twoYearProfit.getMean() + "\n兩年最少獲利:"
					+ twoYearProfit.getMin() + "\n兩年月最高獲利:"
					+ twoYearProfit.getMax() + "\n三年平均獲利:"
					+ threeYearProfit.getMean() + "\n三年最少獲利:"
					+ threeYearProfit.getMin() + "\n三年最高獲利:"
					+ threeYearProfit.getMax() + "\n";
			System.out.println(line);
			writer.write(line);
			writer.close();
		}

		/**
		 * TODO
		 */
		public void analyzePeak() {
			Iterator<String> iter = maxMap.keySet().iterator();
			while (iter.hasNext()) {
				String d1 = iter.next();
				maxMap.get(d1).doubleValue();
			}
		}

		/**
		 * TODO
		 * 
		 * @throws ParseException
		 */
		public void baseAnalyzer() throws ParseException {
			SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMdd");
			GregorianCalendar afterDay = new GregorianCalendar();
			GregorianCalendar beforeDay = new GregorianCalendar();
			Iterator<String> iter = dayProfitMap.keySet().iterator();
			while (iter.hasNext()) {
				boolean isMax = true;
				boolean isMin = true;
				String today = iter.next();
				double todayVal = dayProfitMap.get(today);
				Date d = df.parse(today);
				beforeDay.setTime(d);
				afterDay.setTime(d);
				beforeDay.add(Calendar.DATE, -LOOK_DAY_THRESHOLD);
				afterDay.add(Calendar.DATE, LOOK_DAY_THRESHOLD);

				String[] between = DateUtil.getDateBetween(df1.format(beforeDay
						.getTime()), df1.format(afterDay.getTime()));
				for (int i = 0; i < between.length; i++) {
					String b = df.format(df1.parse(between[i]));
					if (dayProfitMap.containsKey(b)) {
						if (isMax && dayProfitMap.get(b).doubleValue() > todayVal) {
							isMax = false;
						}
						if (isMin && dayProfitMap.get(b).doubleValue() < todayVal) {
							isMin = false;
						}
					}
				}
				if (isMax) {
					maxMap.put(today, todayVal);
				}
				if (isMin) {
					minMap.put(today, todayVal);
				}
			}
		}
}
